package com.module.admin.app.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.module.admin.BaseController;
import com.module.admin.app.pojo.AppApiTest;
import com.module.admin.app.pojo.AppApiTestDtl;
import com.module.admin.app.pojo.AppApiTestLog;
import com.module.admin.app.service.AppApiTestDtlService;
import com.module.admin.app.service.AppApiTestLogService;
import com.module.admin.app.service.AppApiTestService;
import com.module.admin.sys.pojo.SysUser;
import com.system.comm.model.KvEntity;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameStringUtil;
import com.system.comm.utils.FrameTimeUtil;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;

/**
 * app_info的Controller
 * @author admin
 * @date 2020-12-27 20:12:45
 * @version V1.0.0
 */
@Controller
public class AppApiTestController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(AppApiTestController.class);

	@Autowired
	private AppApiTestService appApiTestService;
	@Autowired
	private AppApiTestDtlService appApiTestDtlService;
	@Autowired
	private AppApiTestLogService appApiTestLogService;

	@RequestMapping(value = "/appApiTest/f-view/manager")
	public String manager(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		return "admin/app/appApiTest-manager";
	}
	
	@RequestMapping(value = "/appApiTest/f-view/edit")
	public String edit(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, String aatId, String copyAatId) {
		if(aatId != null) {
			modelMap.put("appApiTest", appApiTestService.get(aatId));
		} else if (FrameStringUtil.isNotEmpty(copyAatId)) {
			AppApiTest appApiTest = appApiTestService.get(copyAatId);
			if (appApiTest != null) {
				appApiTest.setAatId(null);
			}
			modelMap.put("appApiTest", appApiTest);
		}
		return "admin/app/appApiTest-edit";
	}

	@RequestMapping(value = "/appApiTest/f-json/pageQuery")
	@ResponseBody
	public void pageQuery(HttpServletRequest request, HttpServletResponse response, AppApiTest appApiTest) {
		ResponseFrame frame = new ResponseFrame();
		try {
			Page<AppApiTest> page = appApiTestService.pageQuery(appApiTest);
			frame.setBody(page);
			frame.setSucc();
		} catch (Exception e) {
			LOGGER.error("分页获取信息异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appApiTest/f-json/save")
	@ResponseBody
	public void save(HttpServletRequest request, HttpServletResponse response, AppApiTest appApiTest) {
		ResponseFrame frame = null;
		try {
			SysUser user = getSessionUser(request);
			appApiTest.setCreateUserId(user.getUserId());
			frame = appApiTestService.saveOrUpdate(appApiTest);
		} catch (Exception e) {
			LOGGER.error("异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appApiTest/f-json/delete")
	@ResponseBody
	public void delete(HttpServletRequest request, HttpServletResponse response, String aatId) {
		ResponseFrame frame = null;
		try {
			frame = appApiTestService.delete(aatId);
		} catch (Exception e) {
			LOGGER.error("删除异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appApiTest/f-json/findSearch")
	@ResponseBody
	public void findSearch(HttpServletRequest request, HttpServletResponse response,
			String searchString) {
		ResponseFrame frame = new ResponseFrame();
		try {
			List<KvEntity> list = appApiTestService.findSearch(searchString);
			frame.setBody(list);
			frame.setSucc();
		} catch (Exception e) {
			LOGGER.error("获取信息异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appApiTest/f-view/editDtl")
	public String editDtl(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, String aatdId) {
		if(aatdId != null) {
			AppApiTestDtl appApiTestDtl = appApiTestDtlService.get(aatdId);
			modelMap.put("appApiTestDtl", appApiTestDtl);
		}
		return "admin/app/appApiTestDtl-edit";
	}
	
	@RequestMapping(value = "/appApiTest/f-json/pageQueryDtl")
	@ResponseBody
	public void pageQueryDtl(HttpServletRequest request, HttpServletResponse response, AppApiTestDtl appApiTestDtl) {
		ResponseFrame frame = new ResponseFrame();
		try {
			Page<AppApiTestDtl> page = appApiTestDtlService.pageQuery(appApiTestDtl);
			frame.setBody(page);
			frame.setSucc();
		} catch (Exception e) {
			LOGGER.error("分页获取信息异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appApiTest/f-json/saveDtl")
	@ResponseBody
	public void save(HttpServletRequest request, HttpServletResponse response, AppApiTestDtl appApiTestDtl) {
		ResponseFrame frame = null;
		try {
			SysUser user = getSessionUser(request);
			appApiTestDtl.setCreateUserId(user.getUserId());
			frame = appApiTestDtlService.saveOrUpdate(appApiTestDtl);
		} catch (Exception e) {
			LOGGER.error("异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appApiTest/f-json/deleteDtl")
	@ResponseBody
	public void deleteDtl(HttpServletRequest request, HttpServletResponse response, String aatdId) {
		ResponseFrame frame = null;
		try {
			frame = appApiTestDtlService.delete(aatdId);
		} catch (Exception e) {
			LOGGER.error("删除异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}
	
	@RequestMapping(value = "/appApiTest/f-json/test")
	@ResponseBody
	public void test(HttpServletRequest request, HttpServletResponse response, String aatdId) {
		ResponseFrame frame = null;
		try {
			SysUser user = getSessionUser(request);
			String batchNo = FrameTimeUtil.getStrTime();
			frame = appApiTestDtlService.test(aatdId, batchNo, user);
		} catch (Exception e) {
			LOGGER.error("异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}
	@RequestMapping(value = "/appApiTest/f-json/testAll")
	@ResponseBody
	public void testAll(HttpServletRequest request, HttpServletResponse response, String aatId) {
		ResponseFrame frame = null;
		try {
			SysUser user = getSessionUser(request);
			frame = appApiTestDtlService.testAll(aatId, user);
		} catch (Exception e) {
			LOGGER.error("异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appApiTest/f-view/lookReport")
	public String lookReport(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, String aatId) {
		Map<String, Object> data = appApiTestDtlService.lookReport(aatId);
		modelMap.put("data", data);
		return "admin/app/appApiTest-lookReport";
	}

	@RequestMapping(value = "/appApiTest/f-view/getLastLog")
	public String getLastLog(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, String aatdId) {
		if(aatdId != null) {
			AppApiTestLog appApiTestLog = appApiTestLogService.getLast(aatdId);
			modelMap.put("appApiTestLog", appApiTestLog);
		}
		return "admin/app/appApiTestLog-comparison";
	}
	
	@RequestMapping(value = "/appApiTest/f-json/saveTest")
	@ResponseBody
	public void saveTest(HttpServletRequest request, HttpServletResponse response, AppApiTestLog appApiTestLog) {
		ResponseFrame frame = null;
		try {
			frame = appApiTestLogService.update(appApiTestLog);
		} catch (Exception e) {
			LOGGER.error("异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}
}